package mlz.e_lock;

/*
    生产者线程
 */
public class Producer extends Thread{
    private Resource resource;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            //获取锁
            Resource.lock.lock();
            //flag -> true  生产完成,等待消费
            while (Resource.flag){
                try {
                    Resource.prod.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Resource.count++;
            System.out.println("生产了第:" + Resource.count + "个");
            //修改标志位为生产完成
            Resource.flag = true;
            //唤醒消费者阻塞线程中的一个
            Resource.cust.signal();
            Resource.lock.unlock();
        }
    }
}





















